<template>
  <el-form :model="college" :rules="rules" ref="studentForm" label-width="120px" >
    <el-form-item label="学院编号" prop="id" v-if="props.type!=='add'">
      <el-input v-model="college.id" disabled></el-input>
    </el-form-item>
    <el-form-item label="学院名称" prop="name">
      <el-input v-model="college.name" :disabled="props.type==='search'"></el-input>
    </el-form-item>
    <el-form-item label="学院人数" prop="" v-if="props.type!=='add'">
      <el-input v-model="college.totalStudents" disabled></el-input>
    </el-form-item>
    <el-form-item label="专业数量" prop="" v-if="props.type!=='add'">
      <el-input v-model="college.totalMajor" disabled></el-input>
    </el-form-item>
    <el-form-item label="学院班级数量" prop="" v-if="props.type!=='add'">
      <el-input v-model="college.totalClass" disabled></el-input>
    </el-form-item>
    <el-form-item label="专业" v-if="props.type==='search'" >
      <el-select
          filterable
          placeholder="展开"
          style="width: 240px"
      >
        <el-option
            v-for="item in college.majors"
            :key="item.id"
            :label="item.name"
            :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item v-if="props.type!== 'search'">
      <el-button @click="emits('visible', false)">取消</el-button>
      <el-button type="primary" @click="submitForm">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import {onMounted, ref} from "vue";
import request from "@/requests/request.js";
import {idStores} from "@/stores/stores.js";
import {ElMessage} from "element-plus";
const emits = defineEmits('visible')
const props = defineProps({
  type: String
})
const college = ref({
  name: "教育学院",
  id: "5",
  totalStudents: 108,
  totalClass: 4,
  totalMajor: 2,
  majors: []
})
if(props.type === 'add'){
  college.value = {
    collegeName: '',
    collegeCode: '',
    classCount: 0,
    majorCount: 0,
    collegeCount: 0,
    major: '',
    dean: '',
  }
}
const majorOptions = ref([
  '土木', '软工', '计科', '机械', '电子', '自动化',
  '电气', '材料', '化工', '生物', '医学', '外语', '经管',
  '法学', '教育', '历史', '哲学', '数学', '物理', '化学',
  '地理', '体育', '音乐', '美术', '舞蹈', '戏剧', '影视', '新闻', '传媒',
])
const store = idStores()
onMounted(() => {
  request.get('/college/get-college-by-id', {
    params: {
      id: Number(store.id)
    }
  }).then(res => {
    college.value = res.data.data
  })
})
const rules = ref()
const submitForm = () => {
  if (props.type === 'add') {
    request.get('/college/add-college', {
      params: {
        collegeName: college.value.name
      }
    }).then(res => {
        emits('visible', false)
      ElMessage({
        message: res.data.message,
        type: 'success',
      })
    })
  }
  else if (props.type === 'edit') {
    request.post('/college/update-college',
      {
        name: college.value.name,
        collegeId: college.value.id
      }
    ).then(res => {
      emits('visible', false)
      ElMessage({
        message: res.data.message,
        type: 'success',
      })
    })

  }
}
</script>
